package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiyun.mapper.PendingSigningMapper;
import com.jiyun.mapper.SigningInfoMapper;
import com.jiyun.model.sign.PendingSigning;
import com.jiyun.model.sign.SigningInfo;
import com.jiyun.model.sign.dto.PendingSigningDto;
import com.jiyun.model.sign.dto.PendingSigningQuery;
import com.jiyun.service.IPendingSigningService;
import com.jiyun.vo.sign.PendingSigningVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 针对表【-PendingSigning】的业务操作的Service实现类
 *
 * @author ???
 * @since 2024-09-27
 */
@Service
public class PendingSigningServiceImpl extends ServiceImpl<PendingSigningMapper, PendingSigning> implements IPendingSigningService {

    @Override
    public IPage<PendingSigning> getPageList(PendingSigningQuery query){
        //1.创建查询条件
        QueryWrapper<PendingSigning> qw = new QueryWrapper<>();
        //ToDo 查询条件

        //2.组件分页数据
        IPage<PendingSigning> page = new Page<>(query.getCurrentPage(), query.getPageSize());
        //3.查询
        baseMapper.selectPage(page,qw);
        //4.返回
        return page;
    }

    @Override
    public PageInfo<PendingSigningVo> findAllPendingSigning(Integer flag, PendingSigningDto pendingSigningDto) {
        List<Integer> statusList = new ArrayList<>();
        if (pendingSigningDto.getStatus()!=null){
            statusList.add(pendingSigningDto.getStatus());
        }else {
            if (flag==1){
                statusList.add(0);
                statusList.add(1);
                statusList.add(2);
            }else {
                statusList.add(3);
                statusList.add(4);
            }
        }
        pendingSigningDto.setStatusList(statusList);
        PageHelper.startPage(pendingSigningDto.getPageNum(), pendingSigningDto.getPageSize());
        List<PendingSigningVo> list= baseMapper.findAllPendingSigning(pendingSigningDto);
        return new PageInfo<>(list);
    }

    @Resource
    private SigningInfoMapper signingInfoMapper;
    @Override
    public Boolean ApprovedByreView(Integer pendingSigningId) {
        PendingSigning pendingSigning = new PendingSigning();
        pendingSigning.setId(pendingSigningId);
        pendingSigning.setStatus(1);
        baseMapper.updateById(pendingSigning);
        LambdaQueryWrapper<SigningInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SigningInfo::getPendingSigningId,pendingSigningId);
        SigningInfo signingInfo = signingInfoMapper.selectOne(wrapper);
        if (signingInfo == null){
            return false;
        }
        signingInfo.setStatus(1);
        signingInfoMapper.updateById(signingInfo);
        return true;
    }

    @Override
    public Boolean reject(Integer pendingSigningId, String reasonForRejection) {
        PendingSigning pendingSigning = new PendingSigning();
        pendingSigning.setId(pendingSigningId);
        pendingSigning.setStatus(2);
        baseMapper.updateById(pendingSigning);
        LambdaQueryWrapper<SigningInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SigningInfo::getPendingSigningId,pendingSigningId);
        SigningInfo signingInfo = signingInfoMapper.selectOne(wrapper);
        if (signingInfo == null){
            return false;
        }
        signingInfo.setStatus(2);
        signingInfo.setReasonForRejection(reasonForRejection);
        signingInfoMapper.updateById(signingInfo);
        return true;
    }

    @Override
    public List<PendingSigningVo> ExceptionByids(List<Integer> list) {
        return baseMapper.ExceptionByids(list);
    }
}
